﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public string SpilID = "";
    public string Format = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        using (INewsService service = ServiceFactory.NewsService)
        {
            News item = null;
            if (!String.IsNullOrEmpty(Request["NewsId"]))
            {
                item = service.GetByNewsId(new Guid(Request["NewsId"]))[0];
            }
            else if (!String.IsNullOrEmpty(Request["id"]))
            {
                item = service.GetById(int.Parse(Request["id"]));
            }

            lblTitel.Text = item.Title;
            lblHeader.Text = item.Header;
            lblTekst.Text = Regex.Replace(item.Text.ToString(), Environment.NewLine, "<br />");

            using (IWriterService writerService = ServiceFactory.WriterService)
            {
                Writer writer = writerService.Find(item.Author);
                lblForfatter.Text = (writer == null) ? "Ukendt forfatter" : writer.LogOnName;
            }

            lblEmne.Text = HelperUtilities.GetNewsTypeName(item.NewsType);
            lblDato.Text = item.NewsDate.ToLongDateString() + " " + item.NewsDate.ToString("HH:mm");

            if (!String.IsNullOrEmpty(item.Source))
            {
                lblKilde.Text = "Kilde: " + item.Source;
            }

            #region Image
            string filename = ResolveUrl("~/news/" + item.NewsId.ToString() + ".");
            if (File.Exists(Server.MapPath(filename + "png")))
            {
                imgCover.ImageUrl = filename + "png";
                imgCover.Width = 200;
            }
            else if (File.Exists(Server.MapPath(filename + "jpg")))
            {
                imgCover.ImageUrl = filename + "jpg";
                imgCover.Width = 200;
            }
            else
            {
                imgCover.Visible = false;
            }
            #endregion

            Guid relatedGameId = Guid.Empty;
            string relatedGameTitle = "";

            using (IGameService gameService = ServiceFactory.GameService)
            {
                Game[] relatedGames = gameService.GetAllByNewsRelation(item.NewsId);
                if (relatedGames.Length > 0)
                {
                    DivRelSpil.InnerHtml = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"infotable\">";

                    foreach (Game relatedGame in relatedGames)
                    {
                        if (relatedGame.Title != relatedGameTitle)
                        {
                            DivRelSpil.InnerHtml += "<tr><td class=\"TD_Top10\"><a style=\"color:black;\" href=\"" + ResolveUrl(UrlUtillity.GetGameUri(relatedGame)) + "\">" + relatedGame.Title + "</a></td></tr>";
                            relatedGameId = relatedGame.GameId;
                        }

                        relatedGameTitle = relatedGame.Title;
                    }

                    DivRelSpil.InnerHtml += "</table>";
                }
                else
                {
                    DivRelSpil.Visible = false;
                    DivRelSpilHeader.Visible = false;
                }

                if (item.ShowRelatedScreenshots)
                {
                    GameScreenshot[] screenshots = gameService.GetAssociatedScreenshots(relatedGameId);

                    if (screenshots.Length > 0)
                    {
                        imgScreen1.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(screenshots[0]);
                        HyperLink1.NavigateUrl = UrlUtillity.GetGameScreenshotUri(screenshots[0]);
                    }
                    if (screenshots.Length > 1)
                    {
                        imgScreen2.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(screenshots[1]);
                        HyperLink2.NavigateUrl = UrlUtillity.GetGameScreenshotUri(screenshots[1]);
                    }
                    if (screenshots.Length > 2)
                    {
                        imgScreen3.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(screenshots[2]);
                        HyperLink3.NavigateUrl = UrlUtillity.GetGameScreenshotUri(screenshots[2]);
                    }
                    if (screenshots.Length > 3)
                    {
                        imgScreen4.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(screenshots[3]);
                        HyperLink4.NavigateUrl = UrlUtillity.GetGameScreenshotUri(screenshots[3]);
                    }
                    if (screenshots.Length > 4)
                    {
                        lnkScreens.NavigateUrl = UrlUtillity.GetGameScreenshotUri(screenshots[4]);
                    }
                }
                else
                {
                    DivScreens.Visible = false;
                }
            }
            HtmlMeta HeaderDescription = new HtmlMeta();
            HeaderDescription.Name = "description";
            HeaderDescription.Content = lblHeader.Text.ToString() + "...";
            this.Header.Controls.Add(HeaderDescription);
            this.Title = "Nyhed - " + item.Title;
        }
    }
}
